package divasoft.client.forms;

import dvsdk.GlobalConfig;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import org.jdesktop.application.Action;

/**
 *
 * @author Develop
 */
public class DebugConsole extends javax.swing.JFrame {

    /** Creates new form DebugConsole */
    public DebugConsole() {
        initComponents();
        jCheckBoxMenuItem1.setSelected(GlobalConfig.REDIRECT_OUT_ERR);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        hideButton = new javax.swing.JMenu();
        debugLevel1 = new javax.swing.JRadioButtonMenuItem();
        debugLevel2 = new javax.swing.JRadioButtonMenuItem();
        debugLevel3 = new javax.swing.JRadioButtonMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mulis.MulisApp.class).getContext().getResourceMap(DebugConsole.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR));
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        hideButton.setText(resourceMap.getString("hideButton.text")); // NOI18N
        hideButton.setName("hideButton"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(mulis.MulisApp.class).getContext().getActionMap(DebugConsole.class, this);
        debugLevel1.setAction(actionMap.get("changeLevel1")); // NOI18N
        buttonGroup1.add(debugLevel1);
        debugLevel1.setSelected(true);
        debugLevel1.setText(resourceMap.getString("debugLevel1.text")); // NOI18N
        debugLevel1.setName("debugLevel1"); // NOI18N
        hideButton.add(debugLevel1);

        debugLevel2.setAction(actionMap.get("changeLevel2")); // NOI18N
        buttonGroup1.add(debugLevel2);
        debugLevel2.setText(resourceMap.getString("debugLevel2.text")); // NOI18N
        debugLevel2.setName("debugLevel2"); // NOI18N
        hideButton.add(debugLevel2);

        debugLevel3.setAction(actionMap.get("debugLevelOff")); // NOI18N
        buttonGroup1.add(debugLevel3);
        debugLevel3.setText(resourceMap.getString("debugLevel3.text")); // NOI18N
        debugLevel3.setName("debugLevel3"); // NOI18N
        hideButton.add(debugLevel3);

        jSeparator2.setName("jSeparator2"); // NOI18N
        hideButton.add(jSeparator2);

        jCheckBoxMenuItem1.setAction(actionMap.get("redirect")); // NOI18N
        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText(resourceMap.getString("jCheckBoxMenuItem1.text")); // NOI18N
        jCheckBoxMenuItem1.setName("jCheckBoxMenuItem1"); // NOI18N
        hideButton.add(jCheckBoxMenuItem1);

        jSeparator1.setName("jSeparator1"); // NOI18N
        hideButton.add(jSeparator1);

        jMenuItem2.setAction(actionMap.get("send_back")); // NOI18N
        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        hideButton.add(jMenuItem2);

        jMenuItem1.setAction(actionMap.get("debug_console")); // NOI18N
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        hideButton.add(jMenuItem1);

        jMenuBar1.add(hideButton);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void hide_me() {
        this.hide();
    }
    /*
    @Override
    public void setVisible(boolean b) {
    super.setVisible(b);
    }*/
    
    @Action
    public void changeLevel1() {
        GlobalConfig.DEBUG = 1;
        GlobalConfig.saveConfigToFile();
    }
    
    @Action
    public void changeLevel2() {
        GlobalConfig.DEBUG = 2;
        GlobalConfig.saveConfigToFile();
    }
    
    @Action
    public void debugLevelOff() {
        GlobalConfig.DEBUG = 0;
        GlobalConfig.saveConfigToFile();
    }
    
    public void redirectSystem() {
        try {
            if (!GlobalConfig.REDIRECT_OUT_ERR) return;
            System.out.println("<<< [Redirect System.out/System.err into DebugConsole] >>>");
            final PipedInputStream pis = new PipedInputStream();
            PipedOutputStream pos = new PipedOutputStream(pis);  // throws IOException
            PrintStream ps = new PrintStream(pos);
            System.setOut(ps);
            System.setErr(ps);
            Thread t = new Thread(
                    new Runnable() {

                        @Override
                        public void run() {
                            try {
                                byte[] buf = new byte[8192];
                                int bytesRead = 0;
                                while ((bytesRead = pis.read(buf)) != -1) {
                                    jTextArea1.append(new String(buf, 0, bytesRead));
                                }
                            } catch (Exception ioe) {
                                //ioe.printStacktrace();
                                System.err.println("Fatal Error:  Exiting reader.");
                            } finally {
                                try {
                                    pis.close();
                                } catch (Exception e) {
                                }
                            }
                        }
                    });
            t.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void redirect() {
        GlobalConfig.REDIRECT_OUT_ERR=jCheckBoxMenuItem1.isSelected();
        GlobalConfig.saveConfigToFile();
        redirectSystem();
    }

    @Action
    public void send_back() {
        //JOptionPane.showMessageDialog(this, (UtilMail.sendBackMail(UtilMail.getEmail(), "Console Log", jTextArea1.getText().trim()))?"OK":"Ошибка при отправке","Статус",JOptionPane.INFORMATION_MESSAGE);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButtonMenuItem debugLevel1;
    private javax.swing.JRadioButtonMenuItem debugLevel2;
    private javax.swing.JRadioButtonMenuItem debugLevel3;
    private javax.swing.JMenu hideButton;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
}
